package com.yiyuan.app.dao;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.guaike.core.mybatis.plugins.BaseDAO;
import com.yiyuan.app.entity.po.MRecordPinePO;
import com.yiyuan.app.entity.result.AppUser;
import com.yiyuan.app.mapper.MRecordPineExtMapper;
import com.yiyuan.app.mapper.MRecordPineMapper;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Repository;

import java.time.LocalDate;

@Repository
@AllArgsConstructor
public class MRecordPineDAO extends BaseDAO<MRecordPineMapper, MRecordPinePO> {

    private final MRecordPineExtMapper mRecordPineExtMapper;

    public MRecordPinePO findUserPine(LocalDate startOfWeek, LocalDate endOfWeek, Long userId) {
        LambdaQueryWrapper<MRecordPinePO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(MRecordPinePO::getStartDate, startOfWeek);
        queryWrapper.eq(MRecordPinePO::getEndDate, endOfWeek);
        queryWrapper.eq(MRecordPinePO::getUserId, userId);
        return getBaseMapper().selectOne(queryWrapper);
    }

    public Page<MRecordPinePO> findPage(AppUser user, Integer pageIndex, Integer pageSize) {
        LambdaQueryWrapper<MRecordPinePO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(MRecordPinePO::getUserId, user.getUserId());
        queryWrapper.orderByDesc(MRecordPinePO::getId);
        return getBaseMapper().selectPage(new Page<>(pageIndex, pageSize), queryWrapper);
    }

    public int addPinecone(Long id, Integer pinecone) {
        return mRecordPineExtMapper.addPinecone(id, pinecone);
    }
}
